<?php
session_start();
require_once('../include/connect.inc.php');
		$choice=$_POST[choice]; //ตัวเลือก ช้อย
		$set_id=$_POST[set_id];//รหัสชุด
		$r_student_id=$_SESSION['id'];//รหัสนิสิต
		$IDLC=$_POST[IDLC];//รับค่ามาจากหรัสหัวข้อ
		$q_id=$_POST[q_id];
		$type=$_POST[type];
		$question_id=$_POST[question_id];
		$dates=date("Y-m-d");
		$answer_word=$_POST[answord];
		//ตัวแปลในส่วนของการคิดคะแนน
		$score1=0;
		$score2=0;
		$score3=0;
		$score4=0;
		$score5=0;
		$sumquestion1=0;
		$sumquestion2=0;
		$sumquestion3=0;
		$sumquestion4=0;
		$sumquestion5=0;
		//ตัวแปลในส่วนของการคิดคะแนนหลายตัวเลือก
		$p=0;
		$choiceT=0;
		$sumChoT=0;
		$sumChoF=0;
		$doT=0;
		$doF=0;
?>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//ส่วนของการบันทึกการทำข้อสอบ
$strSql="INSERT INTO tb_dotest (date_test,r_student_id,set_id) VALUES ('$dates','$r_student_id','$set_id')";
	$rs = execute($strSql);
$query= query("SELECT dotest_id FROM tb_dotest order by dotest_id desc limit 0,1");
	while ($rs = fetch_array($query)) {
		$top_dotest_id=$rs['dotest_id'];
		}
		for ($i=0;$i<count($choice);$i++){
		$strSql="INSERT INTO tb_dotest_detail (choice_id,dotest_id,match_ans,answer_word) VALUES ('".$choice[$i]."','".$top_dotest_id."','".$q_id[$i]."','".$answer_word[$i]."')";
		$rs = execute($strSql);
		}
		for ($j=0;$j<count($q_id);$j++){
$strSql2="INSERT INTO tb_dotest_detail_q (question_id,answer_word,dotest_id,type_id) VALUES ('".$q_id[$j]."','".$answer_word[$j]."','".$top_dotest_id."','".$type[$j]."')";
		$rs = execute($strSql2);
		}
?>

<?php
//ส่วนของการคิดคะแนน
$query0= query("SELECT * FROM  tb_dotest_detail_q where dotest_id='$top_dotest_id'");
		while ($rs0= fetch_array($query0)){
			$type=$rs0['type_id'];
			}
?>

<? if($type=2){?>
<?php
echo "ตรวจข้อสอบหลายตัวเลือก..........!!!!<br>";
$sum_multi=0;
$query11= query("SELECT * FROM tb_dotest_detail_q,tb_question where tb_dotest_detail_q.question_id=tb_question.question_id and tb_question.type_id='2' and tb_dotest_detail_q.dotest_id='$top_dotest_id' ");
		while ($rs11= fetch_array($query11)) {
			$question_id11=$rs11['question_id'];
			$p++;
			
$query22= query("SELECT * FROM  tb_choice where question_id=$question_id11");
		while ($rs22= fetch_array($query22)){
			$choice_id22=$rs22['choice_id'];
			$choiceT++;
$query33= query("SELECT * FROM tb_dotest_detail,tb_choice where tb_dotest_detail.choice_id=tb_choice.choice_id and tb_choice.answer=1 and tb_choice.choice_id=$choice_id22 and tb_dotest_detail.dotest_id='$top_dotest_id' ");
		while ($rs33= fetch_array($query33)){
			$choice_id33=$rs33['choice_id'];
			$doT++;
		}
$query66= query("SELECT * FROM tb_dotest_detail,tb_choice where tb_dotest_detail.choice_id=tb_choice.choice_id and tb_choice.answer=0 and tb_choice.choice_id=$choice_id22 and tb_dotest_detail.dotest_id='$top_dotest_id' ");
		while ($rs66= fetch_array($query66)){
			$choice_id66=$rs66['choice_id'];
			$doF++;
			}
		}
$query44= query("SELECT * FROM  tb_choice where question_id=$question_id11 and answer=1");
		while ($rs44= fetch_array($query44)){
			$choice_id44=$rs44['choice_id'];
				$sumChoT++;
			}
$query55= query("SELECT * FROM  tb_choice where question_id=$question_id11 and answer=0");
		while ($rs55= fetch_array($query55)){
			$choice_id55=$rs55['choice_id'];
				$sumChoF++;
			}
}
?>
<?php 
//echo "คะแนนรวม";
//echo $p;
//echo "<br>";
//echo "ตัวเลือกทั้งหมด";
//echo $choiceT;
//echo "<br>";
//echo "ถูกรวม";
//echo $sumChoT;
//echo "<br>";
//echo "ผิดรวม";
//echo $sumChoF;
//echo "<br>";
//echo "ทำถูก";
//echo $doT;
//echo "<br>";
//echo "ทำผิด";
//echo $doF;
//echo "<br>";

//echo "ข้อถูกที่ไม่ได้ทำ";
$NoT=$sumChoT-$doT;
//echo $NoT;
//echo "<br>";
//echo "ข้อผิดที่ไม่ได้ทำ";
$NoF=$sumChoF-$doF;
//echo $NoF;
//echo "<br><br>";

//echo "ส่วนของการคำนวนหาน้ำหนักแต่ละข้อ";
//echo "<br>";

//echo "ค่าคะแนนข้อถูกคือ";
$Rf=$p/$sumChoT;
//echo $Rf;
//echo "<br>";

//echo "ค่าคะแนนข้อผิดคือ";
$Wf=$p/$sumChoF;
//echo $Wf;
//echo "<br>";

//echo "ส่วนของการคำนวนหาคะแนน";
$M=$p-(($NoT*$Rf)+($doF*$Wf));
//echo "<br>";
//echo "คะแนนที่ทำได้ =";
//echo $M=number_format($M,2,'.',' ');
$score2=$score2+$M;
//echo"<br><br>";
?>
<? }?>

<? if($type==4){?>
<? 
$query4= query("SELECT * FROM tb_dotest_detail_q,tb_question where tb_dotest_detail_q.question_id=tb_question.question_id and tb_question.type_id='4' and tb_dotest_detail_q.dotest_id='$top_dotest_id'");
			while ($rs4 = fetch_array($query4)) {
				$question_id4=$rs4['question_id'];
				echo "คำตอบ : ";
				echo $answer_word=$rs4['answer_word'];
				echo "<br>";
				$sumquestion4++;
				$sum++;
$query44=query("SELECT * FROM tb_choice where question_id='$question_id4' ");
			while ($rs44= fetch_array($query44)) {
				$choice_id=$rs44['choice_id'];
				echo "เฉลยคือ : ";
				echo $choice_detail=$rs44['choice_detail'];
				echo "<br>";
					if ($answer_word!=$choice_detail){
						echo "คำตอบผิดครับ!!!";
					}else if($answer_word=$choice_detail){
						echo "ถูกต้องแล้วจ้า!!!";
						$score4++;
					}
					echo "<br>";
				}
			}
?>

<? }else{?>
<? 
$sum=0;
$query =query ("SELECT
tb_topic.subject_id,
tb_dotest_detail.choice_id,
tb_dotest_detail.match_ans,
tb_choice.question_id,
tb_question.topic_id,
tb_subject.subject_name,
tb_choice.choice_detail,
tb_question.type_id,
tb_question.question_detail,
tb_topic.topic_detail,
tb_choice.answer,
tb_type.type_name
FROM
tb_dotest_detail
Inner Join tb_choice ON tb_dotest_detail.choice_id = tb_choice.choice_id
Inner Join tb_question ON tb_question.question_id = tb_choice.question_id
Inner Join tb_topic ON tb_topic.topic_id = tb_question.topic_id
Inner Join tb_type ON tb_type.type_id = tb_question.type_id
Inner Join tb_subject ON tb_topic.subject_id = tb_subject.subject_id where dotest_id='$top_dotest_id' ");
	while ($rs = fetch_array($query)) {
		$choice_id=$rs['choice_id'];
		$subject_name=$rs['subject_name'];
		$topic_detail=$rs['topic_detail'];
		$choice_detail=$rs['choice_detail'];
		$type_name=$rs['type_name'];
		$type_id=$rs['type_id'];
		$answer=$rs['answer'];
		$match_ans=$rs['match_ans'];
?>

<?php
if  ($type_id==1){
$query1= query("SELECT * FROM tb_choice,tb_question where tb_choice.question_id=tb_question.question_id and tb_question.type_id='1' and tb_choice.choice_id='$choice_id' ");
			while ($rs1 = fetch_array($query1)) {	
				$choice_id=$rs1['choice_id'];
				$choice_detail=$rs1['choice_detail'];
				$question_id=$rs1['question_id'];
				$question_detail=$rs1['question_detail'];
				$type_id=$rs1['type_id'];
				$answer1=$rs1['answer'];
				 $sumquestion1++;
				 $sum++;
					 if ($answer1!=1){
						$ans='เป็นคำตอบที่ผิด';
					}else if ($answer1==1){
						$ans='ถูกต้องนะครับ';
						$score1++;
					}
				}
			}
?>

<?php
if  ($type_id==3){
$query3= query("SELECT * FROM tb_choice,tb_question where tb_choice.question_id=tb_question.question_id and tb_question.type_id='3' and tb_choice.choice_id='$choice_id' ");
			while ($rs3 = fetch_array($query3)) {	
				$choice_id=$rs3['choice_id'];
				$choice_detail=$rs3['choice_detail'];
				$question_id=$rs3['question_id'];
				$question_detail=$rs3['question_detail'];
				$type_id=$rs3['type_id'];
				$answer3=$rs3['answer'];
				 $sumquestion3++;
				 $sum++;
					 if ($answer3!=1){
						$ans='เป็นคำตอบที่ผิด';
					}else if ($answer3==1){
						$ans='ถูกต้องนะครับ';
						$score3++;
					}
				}
			}
?>

<?
if  ($type_id ==5){
$query5= query("SELECT * FROM tb_choice,tb_question where tb_question.question_id='$match_ans' and tb_question.type_id='5' and tb_choice.choice_id='$choice_id' ");
			while ($rs5= fetch_array($query5)) {	
				 $choice_id=$rs5['choice_id'];
				 $question_id=$rs5['question_id'];
				 $choice_detail=$rs5['choice_detail'];
				 $question_detail=$rs5['question_detail'];
				 $type_id=$rs5['type_id'];
				 $answer5=$rs5['answer'];
				 $sumquestion5++;
				 $sum++;
					if ($answer5!=$match_ans){
						$ans='เป็นคำตอบที่ผิด';
					}else if ($answer5==$match_ans){
						$ans='ถูกต้องนะครับ';
						$score5++;
					}
				}
			}
?>

<? if($type_id==2){
	echo "";
	}else{
?>
<?php /*?><font color="#0000FF">ประเภท : </font><? echo $type_name;?></br>
<font color="#0000FF">คำถาม คือ : </font><? echo $question_detail;?></br>
<font color="#0000FF">ตัวเลือกที่ทำไป คือ : </font><? echo $choice_detail;?></br>
<font color="#0000FF">เป็นคำตอบที่ : </font><? echo $ans;?></br></br><?php */?>
<? }?>
<? }?>
<? }?>
</font></div>
<form id="form1" name="form1" method="post" action="save_score.php">
<font size="+1">
<table width="33%" border="1" cellpadding="0" cellspacing="0"  bordercolor="80300f" >
  <tr>
    <td width="48%" align="right" >ข้อสอบชุดที่ :</td>
<?php
 $query= query("SELECT set_id FROM tb_dotest where dotest_id='$top_dotest_id' ");
 		while ($rs= fetch_array($query)) {
			$set_id=$rs['set_id'];
?>
    <td width="52%" ><span class="style2"><?php echo $set_id;?></span></td>
    <input type="hidden" name="set_id" id="set_id" value="<?php echo $set_id;?>" />
<?php }?>
  </tr>
  <tr>
    <td align="right"><span class="style2">วิชา : </span></td>
    <td><span class="style2"><?php echo $subject_name;?></span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">หัวข้อสอบ : </span></td>
    <td><span class="style2"><?php echo $topic_detail;?></span></td>
  </tr>
    <tr>
    <td align="right"><span class="style2">คะแนน : ข้อ : </span></td>
<?php
 	$query= query("SELECT point FROM tb_set_of_test where set_id='$set_id'");
		while ($rs= fetch_array($query)) {
			$point=$rs['point'];
?>
    <td><span class="style2"><? echo $point;?></span></td>
    <input type="hidden" name="point" id="point" value="<?php echo $point;?>" />
<?php }?>
  </tr>
  <tr>
    <td align="right"><span class="style2">ตัวเลือกเดียว : </span></td>
    <td>
      <span class="style2">
      <?php 
	if ($score1==0){
		echo"ไม่มีคะแนน";
		}else{
		$score1=$score1*$point;
		 echo "ทำได้ : $score1";?> 
      คะแนน
         <?php }?>
      </span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">หลายตัวเลือก : </span></td>
    <td> <?php echo $multi_point;?>
      <span class="style2">
      <?php 
	if ($score2==0){
		echo "ไม่มีคะแนน";
		}else{
		$score2=$score2*$point;
		 echo "ทำได้ : $score2";?> 
      คะแนน
         <?php }?>
      </span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">ข้อสอบถูก-ผิด : </span></td>
    <td>
      <span class="style2">
      <?php 
	if ($score3==0){
		echo"ไม่มีคะแนน";
		}else{
		$score3=$score3*$point;
		 echo "ทำได้ : $score3";?> 
      คะแนน
         <?php }?>
      </span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">ข้อสอบเติมคำ : </span></td>
    <td>
      <span class="style2">
      <?php 
	if ($score4==0){
		echo"ไม่มีคะแนน";
		}else{
		$score4=$score4*$point;
		 echo "ทำได้ : $score4";?> 
      คะแนน
         <?php }?>
      </span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">ข้อสอบจับคู่ : </span></td>
    <td>
      <span class="style2">
      <?php 
	if ($score5==0){
		echo"ไม่มีคะแนน";
		}else{
		$score5=$score5*$point;
		 echo "ทำได้ : $score5";?> 
      คะแนน
         <?php }?>
      </span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">จำนวนข้อ : </span></td>
<?php
$sum_q=0;
$query=query("SELECT question_id FROM tb_dotest_detail_q where dotest_id='$top_dotest_id' ");
	while ($rs= fetch_array($query)){
		$q=$rs['question_id'];
		$sum_q++;
		}
?>
    <td><span class="style2"><? echo "$sum_q";?> ข้อ</span></td>
  </tr>
  <tr>
    <td align="right"><span class="style2">คะแนนเต็ม :</span></td>
<?php /*?><?php $sumquestion=$sumquestion1+$p+$sumquestion3+$sumquestion4+$sumquestion5;
$sumquestion=$sumquestion*$point;?><?php */?>
<?php
$sum_ques=0;
$query=query("SELECT question_id FROM tb_dotest_detail_q where dotest_id='$top_dotest_id'");
	while ($rs= fetch_array($query)){
		$q=$rs['question_id'];
		$sum_ques++;
		}
		$sum_ques=$sum_ques*$point;
?>
    <td><span class="style2"><? echo "$sum_ques";?>  คะแนน</span></td>
    <input type="hidden" name="max_score" id="max_score" value="<?php echo "$sum_ques";?>" />
  </tr>
  <tr>
    <td align="right"><span class="style2">รวมคะแนนที่ทำได้ : </span></td>
<?php $sumscore=$score1+$score2+$score3+$score4+$score5;?>
<input type="hidden" name="sumscore" id="sumscore" value="<? echo "$sumscore";?>" />
<input type="hidden" name="dotest_id" id="dotest_id" value="<? echo $_GET[dotest_id];?>" />
    <td>
    <span class="style2"><?php echo "$sumscore";?> คะแนน</span></td>
  </tr>
</table>
<?php
$strSql="INSERT INTO  tb_score (r_student_id,sumscore,max_score,set_id,dotest_id,dates) VALUES ('$r_student_id','$sumscore','$sum_ques','$set_id','$top_dotest_id','$dates')";
	$rs = execute($strSql);
			sleep(1);// หน่วงเวลาไว้ 1 วิ
			echo "<script>window.close();//</script>";
		/*echo "<script>window.location='../show_dotest_result.php';//</script>";*/
		/*$ProcessOutput .= "<BR><BR>";
		$ProcessOutput .= "<FONT COLOR=\"#336600\"><B>ได้ทำการบันทึกข้อมูลเรียบร้อยแล้ว </B></FONT><BR><BR>";
		$ProcessOutput .= "</CENTER>";
		$ProcessOutput .= "<BR><BR>";*/
?>